package days5.easy.title14;

/**
 * @Author fengzihao
 * @Date 2024-04-19 21:56
 */
class Solution {
    // public static void main(String[] args) {
    //     String[] strs = {"flower","flower","flower","flower"};
    //     System.out.println(longestCommonPrefix(strs));
    // }
    public String longestCommonPrefix(String[] strs) {
        int flag_1 = 0;
        if (strs[0].isEmpty())
            return "";
        else if (strs.length < 2) {
            return strs[0];
        } else {
            int count = 0;
            for (int i = 0; i < strs[0].length(); i++) {
                char flag = strs[0].charAt(i);
                int flag_break = 0;
                for (int j = 1; j < strs.length; j++) {
                    if (strs[j].isEmpty())
                        return "";
                    if (strs[j].length() > i) {
                        if (flag != strs[j].charAt(i)) {
                            flag_break = 1;
                            flag_1 = 1;
                            break;
                        }
                    } else {
                        flag_1 = 1;
                        break;
                    }
                }
                count++;
                if (flag_break == 1)
                    break;

            }
            if (count == strs[0].length() && flag_1 == 0)
                count++;
            return strs[0].substring(0, count - 1);
        }
    }
}